package com.baizhi.service.impl;

import com.baizhi.constants.RedisPrefix;
import com.baizhi.dao.UserDao;
import com.baizhi.dto.UpdateUserDTO;
import com.baizhi.entity.User;
import com.baizhi.po.FeedsUserPO;
import com.baizhi.po.OneUserPO;
import com.baizhi.po.QueryByIdUserPO;
import com.baizhi.po.UserPO;
import com.baizhi.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;

/**
 * 用户(User)表服务实现类
 *
 * @author makejava
 * @since 2021-09-26 00:06:57
 */
@Service("userService")
@Transactional
@RabbitListener(queuesToDeclare = @Queue("login"))
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    private static final Logger log1 = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public User login(String phone) {
        //测试消费者
        UserServiceImpl.haha(phone);
        //头像
        String avatars = UserServiceImpl.getAvatars();
        System.out.println("avatars = " + avatars);
        User user = null;
        //条件
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, phone);
        //值查询没有删除的
        //查询用户是否存在
        user = userDao.selectOne(queryWrapper);
        Date deletedAt = user.getDeletedAt();
        if (deletedAt != null) {
            throw new RuntimeException("您的账号因违法规定被删除，请规范您的行为");
        }
        if (user == null) {
            //创建用户
            user = new User();
            user.setName(phone);
            user.setAvatar(avatars);
            user.setPhone(phone);
            user.setFollowersCount(0);
            user.setFollowingCount(0);
            user.setIntro("");
            user.setCreatedAt(new Date());
            user.setUpdatedAt(new Date());
            int insert = userDao.insert(user);
            //添加完重新给用户赋值
            user = userDao.selectOne(queryWrapper);
        }
        //返回用户信息
        return user;
    }

    @Override
    public QueryByIdUserPO queryById(Integer id) {
        User user = userDao.selectById(id);
        QueryByIdUserPO queryByIdUserPO = new QueryByIdUserPO();
        BeanUtils.copyProperties(user, queryByIdUserPO);
        return queryByIdUserPO;

    }

    @Override
    public UserPO updateUser(Integer id, UpdateUserDTO updateUserDTO) {
        User entity = new User();
        String s = "-1";
        entity.setId(id);
        //查看是否有手机号
        if (updateUserDTO.getPhone() != null) {
            if (updateUserDTO.getCaptcha() == null) {
                throw new RuntimeException("请填写手机验证码");
            }
            s = redisTemplate.opsForValue().get(RedisPrefix.MSG_PREFIX + updateUserDTO.getPhone()).toString();
            if (!s.equals(updateUserDTO.getCaptcha())) {
                throw new RuntimeException("手机验证码不正确");
            }
        }
        entity.setPhone(updateUserDTO.getPhone());
        if (updateUserDTO.getName() != null) {
            entity.setName(updateUserDTO.getName());
        }
        if (updateUserDTO.getIntro() != null) {
            entity.setIntro(updateUserDTO.getIntro());
        }
        userDao.updateById(entity);
        //将数据取出来，存储到redis中，并提取
        User user = userDao.selectById(id);
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(user, userPO);
        return userPO;
    }

    @Override
    public String queryByUid(Integer uid) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, uid);
        User user = userDao.selectOne(queryWrapper);
        return user.getName();
    }

    @Override
    public OneUserPO queryOneById(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id);
        User user = userDao.selectOne(queryWrapper);
        OneUserPO oneUserPO = new OneUserPO();
        BeanUtils.copyProperties(user, oneUserPO);
        return oneUserPO;
    }

    /**
     * 为组装动态列表博主up为生
     *
     * @param id
     * @return
     */
    @Override
    public FeedsUserPO queryByUserId(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id);
        User user = userDao.selectOne(queryWrapper);
        FeedsUserPO feedsUserPO = new FeedsUserPO();
        BeanUtils.copyProperties(user, feedsUserPO);
        return feedsUserPO;
    }


    @RabbitHandler
    public static void haha(String message) {
        System.out.println("消费者 = " + message);

    }

    //生成头像
    public static String getAvatars() {
        HashMap<String, String> map = new HashMap<>();
        map.put("avatars", "https://p.qqan.com/up/2020-7/2020071308424438852.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-2/16137866805953840.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-1/202111411186632.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-1/202116132678380.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-2/16137866796094190.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-2/16137866805953840.jpg");
        map.put("avatars", "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3528124961,3599494671&fm=11&gp=0.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-1/2021127112032518.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-1/2021127112032518.jpg");
        map.put("avatars", "https://p.qqan.com/up/2021-2/202128104346789.jpg");
        return map.get("avatars");
    }


}

